#include <wx/wx.h>
#include <wx/utils.h>
#include <wx/file.h>
#include <wx/textfile.h>
#include <wx/dir.h>
#include <iostream>
#include <string>
#include <vector>
#include <map>

class AICommunicationSystem {
private:
    std::vector<std::string> conversation;
    
public:
    void StartInteractive() {
        std::cout << "🚀 AI Communication System v2.0" << std::endl;
        std::cout << "================================" << std::endl;
        std::cout << "Human ↔ AI ↔ Computer" << std::endl;
        std::cout << "Type 'help' for commands, 'exit' to quit" << std::endl;
        std::cout << std::endl;
        
        char buffer[512];
        while (true) {
            std::cout << "👤 Human> ";
            std::flush(std::cout);
            
            if (!std::cin.getline(buffer, sizeof(buffer))) {
                break;
            }
            
            std::string input(buffer);
            if (input == "exit" || input == "quit") {
                std::cout << "👋 Goodbye!" << std::endl;
                break;
            }
            else if (input == "help") {
                ShowHelp();
            }
            else if (input == "status") {
                ShowStatus();
            }
            else if (input == "files") {
                ListFiles();
            }
            else if (input == "wxinfo") {
                ShowWxInfo();
            }
            else if (input == "compile") {
                CompileProject();
            }
            else if (input == "history") {
                ShowHistory();
            }
            else if (!input.empty()) {
                ProcessInput(input);
            }
        }
    }
    
    void ProcessInput(const std::string& input) {
        conversation.push_back("Human: " + input);
        std::cout << "🤖 AI: Analyzing command..." << std::endl;
        
        // AI 处理逻辑
        if (input.find("read") != std::string::npos) {
            std::cout << "📖 File read operation" << std::endl;
            ExecuteComputerCommand("read_file", input);
        }
        else if (input.find("write") != std::string::npos || input.find("create") != std::string::npos) {
            std::cout << "📝 File write operation" << std::endl;
            ExecuteComputerCommand("write_file", input);
        }
        else if (input.find("compile") != std::string::npos || input.find("build") != std::string::npos) {
            std::cout << "🔨 Compile operation" << std::endl;
            ExecuteComputerCommand("compile", input);
        }
        else {
            std::cout << "💭 General command processing" << std::endl;
            ExecuteComputerCommand("general", input);
        }
    }
    
    void ExecuteComputerCommand(const std::string& type, const std::string& command) {
        std::cout << "🖥️ Computer: Executing " << type << " command" << std::endl;
        
        // 使用 wxWidgets 功能执行命令
        wxString cwd = wxGetCwd();
        std::cout << "   Current directory: " << (const char*)cwd.mb_str() << std::endl;
        
        if (type == "read_file") {
            // 模拟文件读取
            std::cout << "   Reading file..." << std::endl;
        }
        else if (type == "write_file") {
            // 模拟文件写入
            std::cout << "   Writing file..." << std::endl;
        }
        else if (type == "compile") {
            // 模拟编译
            std::cout << "   Compiling project..." << std::endl;
        }
        
        conversation.push_back("Computer: " + type + " - " + command);
        std::cout << "✅ Command executed successfully" << std::endl;
    }
    
    void ShowHelp() {
        std::cout << std::endl;
        std::cout << "📖 AI Communication System - Help" << std::endl;
        std::cout << "================================" << std::endl;
        std::cout << "Communication Channels:" << std::endl;
        std::cout << "  👤 Human → 🤖 AI → 🖥️ Computer" << std::endl;
        std::cout << "  🖥️ Computer → 🤖 AI → 👤 Human" << std::endl;
        std::cout << std::endl;
        std::cout << "Available Commands:" << std::endl;
        std::cout << "  read <file>       - Read and analyze a file" << std::endl;
        std::cout << "  write <file>      - Write content to a file" << std::endl;
        std::cout << "  compile <project> - Compile a Code::Blocks project" << std::endl;
        std::cout << "  build <target>    - Build a specific target" << std::endl;
        std::cout << "  status            - Show system status" << std::endl;
        std::cout << "  files             - List project files" << std::endl;
        std::cout << "  wxinfo            - Show wxWidgets information" << std::endl;
        std::cout << "  history           - Show command history" << std::endl;
        std::cout << "  help              - Show this help" << std::endl;
        std::cout << "  exit              - Exit the system" << std::endl;
        std::cout << std::endl;
    }
    
    void ShowStatus() {
        std::cout << std::endl;
        std::cout << "📊 System Status" << std::endl;
        std::cout << "================" << std::endl;
        std::cout << "  AI System: Running" << std::endl;
        std::cout << "  wxWidgets: 3.2.7" << std::endl;
        std::cout << "  Platform: Windows" << std::endl;
        std::cout << "  Current Directory: " << (const char*)wxGetCwd().mb_str() << std::endl;
        std::cout << "  Conversation History: " << conversation.size() << " entries" << std::endl;
        std::cout << "  Communication: Human ↔ AI ↔ Computer" << std::endl;
        std::cout << std::endl;
    }
    
    void ListFiles() {
        std::cout << std::endl;
        std::cout << "📁 Project Files" << std::endl;
        std::cout << "================" << std::endl;
        
        wxString cwd = wxGetCwd();
        wxDir dir(cwd);
        
        if (!dir.IsOpened()) {
            std::cout << "Cannot open directory" << std::endl;
            return;
        }
        
        int cpp_count = 0, h_count = 0, cbp_count = 0;
        
        std::cout << "C++ Source Files:" << std::endl;
        wxString filename;
        bool cont = dir.GetFirst(&filename, "*.cpp", wxDIR_FILES);
        while (cont) {
            std::cout << "  📄 " << (const char*)filename.mb_str() << std::endl;
            cpp_count++;
            cont = dir.GetNext(&filename);
        }
        
        std::cout << "Header Files:" << std::endl;
        cont = dir.GetFirst(&filename, "*.h", wxDIR_FILES);
        while (cont) {
            std::cout << "  📄 " << (const char*)filename.mb_str() << std::endl;
            h_count++;
            cont = dir.GetNext(&filename);
        }
        
        std::cout << "Project Files:" << std::endl;
        cont = dir.GetFirst(&filename, "*.cbp", wxDIR_FILES);
        while (cont) {
            std::cout << "  📁 " << (const char*)filename.mb_str() << std::endl;
            cbp_count++;
            cont = dir.GetNext(&filename);
        }
        
        std::cout << std::endl;
        std::cout << "Summary:" << std::endl;
        std::cout << "  C++ Files: " << cpp_count << std::endl;
        std::cout << "  Header Files: " << h_count << std::endl;
        std::cout << "  Project Files: " << cbp_count << std::endl;
        std::cout << "  Total: " << (cpp_count + h_count + cbp_count) << " files" << std::endl;
        std::cout << std::endl;
    }
    
    void ShowWxInfo() {
        std::cout << std::endl;
        std::cout << "🔧 wxWidgets Information" << std::endl;
        std::cout << "========================" << std::endl;
        std::cout << "  Version: 3.2.7" << std::endl;
        std::cout << "  Unicode: Enabled" << std::endl;
        std::cout << "  Platform: Windows" << std::endl;
        std::cout << "  Compiler: GCC (MinGW)" << std::endl;
        std::cout << "  Library: Dynamic (DLL)" << std::endl;
        std::cout << "  Architecture: 64-bit" << std::endl;
        std::cout << std::endl;
    }
    
    void CompileProject() {
        std::cout << std::endl;
        std::cout << "🔨 Compilation System" << std::endl;
        std::cout << "====================" << std::endl;
        std::cout << "Scanning for project files..." << std::endl;
        
        wxString cwd = wxGetCwd();
        wxDir dir(cwd);
        wxArrayString cbpFiles;
        
        if (dir.IsOpened()) {
            wxString filename;
            bool cont = dir.GetFirst(&filename, "*.cbp", wxDIR_FILES);
            while (cont) {
                cbpFiles.push_back(filename);
                cont = dir.GetNext(&filename);
            }
        }
        
        if (cbpFiles.IsEmpty()) {
            std::cout << "No Code::Blocks project files found." << std::endl;
        } else {
            std::cout << "Found project files:" << std::endl;
            for (size_t i = 0; i < cbpFiles.GetCount(); i++) {
                std::cout << "  📁 " << (const char*)cbpFiles[i].mb_str() << std::endl;
            }
            std::cout << std::endl;
            std::cout << "Use 'compile <project.cbp>' to compile specific project" << std::endl;
        }
        std::cout << std::endl;
    }
    
    void ShowHistory() {
        std::cout << std::endl;
        std::cout << "📜 Communication History" << std::endl;
        std::cout << "========================" << std::endl;
        
        if (conversation.empty()) {
            std::cout << "No commands executed yet." << std::endl;
        } else {
            for (const auto& entry : conversation) {
                std::cout << "  " << entry << std::endl;
            }
        }
        std::cout << std::endl;
    }
};

int main(int argc, char* argv[]) {
    wxInitialize();
    
    AICommunicationSystem aiSystem;
    
    if (argc > 1) {
        std::string arg = argv[1];
        if (arg == "--interactive" || arg == "-i") {
            aiSystem.StartInteractive();
        }
        else if (arg == "--status" || arg == "-s") {
            aiSystem.ShowStatus();
        }
        else if (arg == "--help" || arg == "-h") {
            aiSystem.ShowHelp();
        }
        else if (arg == "--files" || arg == "-f") {
            aiSystem.ListFiles();
        }
        else if (arg == "--wxinfo" || arg == "-w") {
            aiSystem.ShowWxInfo();
        }
        else if (arg == "--compile" || arg == "-c") {
            aiSystem.CompileProject();
        }
        else if (arg == "--version" || arg == "-v") {
            std::cout << "cbp2make_ai v2.0" << std::endl;
            std::cout << "Complete AI Communication System" << std::endl;
            std::cout << "Built with wxWidgets 3.2.7" << std::endl;
        }
        else {
            std::cout << "Unknown option: " << arg << std::endl;
            std::cout << "Use --help for usage information" << std::endl;
        }
    } else {
        // 默认进入交互模式
        aiSystem.StartInteractive();
    }
    
    wxUninitialize();
    return 0;
}
